package com.kethink;

import java.util.Arrays;
import java.util.Stack;

/**
 * @Description create by xuxiaojun
 * date: 2018/12/26
 * Time: 14:09
 */
public class LeeCode_821 {

    public static void main(String[] args){
        String S = "loveleetcode";
        char C = 'e';
        int[] array = shortestToChar(S, C);
        System.out.println(Arrays.toString(array));
    }

    public static int[] shortestToChar(String S, char C) {

        int[] result = new int[S.length()] ;
        char[] chars = S.toCharArray();
        int[] loan = new int[S.length()];
        //当前两数之差
        int x = 0;
        //loan数组存放字符C的位置
        for(int i = 0 ; i < chars.length ; i++){
            if(chars[i] == C){
                loan[x]=i;
                x++;
            }
        }
        for(int i = 0 ; i < chars.length ; i++){
            //y为C与loan里面每一个数比较后的最小值
            int y = Integer.MAX_VALUE;
            if(chars[i] == C){
                result[i] = 0;
            }else{
                //计算每个C的位置与loan数组中的最短距离，遍历次数为loan中非0个数
                for(int j = 0 ; j < x; j++ ){
                    //s为当前字符和loan数组中元素的距离
                    int s = 0;
                    if(i<loan[j]){
                        s = loan[j] - i;
                    }else{
                        s = i - loan[j];
                    }
                    if(s < y){
                        y = s;
                    }
                }
                result[i] = y;
            }
        }
        return result;
    }
}
